package com.gzy.xuexiziliao.controller;


import com.gzy.xuexiziliao.common.result.PageResult;
import com.gzy.xuexiziliao.common.result.Result;
import com.gzy.xuexiziliao.pojo.entity.Doctor;
import com.gzy.xuexiziliao.service.DoctorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 医生管理
 */
@RestController
@Slf4j
@RequestMapping("/doctor/doctors")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;

    @PostMapping
    public Result save(@RequestBody Doctor doctor){
        log.info("新增医生,doctor:{}",doctor);
        doctorService.save(doctor);
        return Result.success("添加成功");
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除，ids:{}",ids);
        doctorService.delete(ids);
        return Result.success("删除成功");
    }

    @PutMapping
    public Result update(@RequestBody Doctor doctor){
        log.info("更新医生信息,doctor:{}",doctor);
        doctorService.update(doctor);
        return Result.success("更新成功");
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询医生：{}",id);
        Doctor doctor=doctorService.getById(id);
        if (doctor==null){
            return Result.error("未找到该医生");
        }
        return Result.success(doctor);
    }
    @GetMapping("/name")
    public Result<List<Doctor>> getByName(@RequestParam String name){
        log.info("根据name查询医生：{}",name);
        List<Doctor> doctor =doctorService.getByName(name);
        if (doctor == null){
            return Result.error("未找到该医生");
        }
        return Result.success(doctor);
    }

    @GetMapping("/list")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        PageResult pageBean=doctorService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    /**
     * 预约医生
     * @param id
     * @return
     */
    @PostMapping("/appointment/{id}")
    public Result Appointment(@PathVariable Integer id){
        log.info("预约医生");
        Doctor doctorinfo = doctorService.getById(id);
        Doctor doctorServiceAppointment = doctorService.Appointment(doctorinfo);
        if (doctorServiceAppointment==null){
            return Result.error("预约失败");
        }
        return Result.success("预约成功");
    }
    //取消预约
    @GetMapping("/cancel/{id}")
    public Result cancelAppointment(@PathVariable Integer id){
        log.info("取消预约");
        Doctor doctorinfo = doctorService.getById(id);
        Doctor doctorServiceAppointment = doctorService.cancelAppointment(doctorinfo);
        if (doctorServiceAppointment==null){
            return Result.error("取消预约失败");
        }
        return Result.success("取消预约成功");
    }
}
